#include "myopenglwidget.h"
#include <QTime>
#include <QImage>

MyOpenGLWidget::MyOpenGLWidget()
{
    //设置上下文属性
    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(3, 3); //OpenGL 版本号3.3
    format.setProfile(QSurfaceFormat::CoreProfile);// 核心模式
    this->setFormat(format);
 }

MyOpenGLWidget::~MyOpenGLWidget()
{
    // Make sure the context is current and then explicitly
    // destroy all underlying OpenGL resources.
    makeCurrent();

    delete _texture;
    delete _shaderProgram;
    delete _vao;
    delete _vbo;
    delete _ebo;

    _vbo->destroy();
    _vao->destroy();
    _ebo->destroy();
    _texture->destroy();

    doneCurrent();
}

void MyOpenGLWidget::initializeGL()
{
    //绑定OpenGL函数指针？类似GLAD库的作用？
    initializeOpenGLFunctions();

    _shaderProgram = new QOpenGLShaderProgram;
    _shaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertex.vert");
    _shaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fragment.frag");
    //链接
    if(!_shaderProgram->link()){
        qDebug() << "link failed!\n";
    }

    // float vertices[] = {
    //     // positions          // colors           // texture coords
    //     0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
    //     0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
    //     -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
    //     -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left
    // };
    // unsigned int indices[] = {
    //     0, 1, 3, // first triangle
    //     1, 2, 3  // second triangle
    // };

    // _vao = new QOpenGLVertexArrayObject();
    // _vao->create();
    // _vao->bind();

    // _vbo = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
    // _vbo->create();
    // if(!_vbo->bind()){
    //     qDebug() << "vbo绑定失败！";
    // }
    // _vbo->allocate(vertices, sizeof(vertices));

    // _ebo = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
    // _ebo->create();
    // if(!_ebo->bind()){
    //     qDebug() << "ebo绑定失败！";
    // }
    // _ebo->allocate(indices, sizeof(indices));

    // // position attribute
    // glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    // glEnableVertexAttribArray(0);
    // // color attribute
    // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    // glEnableVertexAttribArray(1);
    // // texture coord attribute
    // glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    // glEnableVertexAttribArray(2);

    // //创建对象
    // _texture = new QOpenGLTexture(QImage(":/container.jpg").mirrored());
    // _texture->setWrapMode(QOpenGLTexture::Repeat);
    // _texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
    // _texture->setMagnificationFilter(QOpenGLTexture::Linear);
    // _texture->generateMipMaps();
}

void MyOpenGLWidget::paintGL()
{
    //由于继承了QOpenGLFunctions，可以直接使用OpenGL中的函数
    // render
    // ------
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // // bind Texture
    // _texture->bind();

    // update shader uniform
    _shaderProgram->bind();

    // // render the triangle
    // _vao->bind();
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

}

void MyOpenGLWidget::resizeGL(int width, int height) {
    // OpenGL渲染窗口的尺寸大小，glViewport可以设置位置和宽高
    //    glViewport(0, 0, w, h);
    Q_UNUSED(width);
    Q_UNUSED(height);

    const qreal retinaScale = devicePixelRatio();
    glViewport(0, 0, this->width() * retinaScale, this->height() * retinaScale); //视口的宽度和高度将根据设备的像素比例进行缩放
}
